package com.y.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.y.entity.Food;
import com.y.entity.Kind;
import com.y.entity.Order;
import com.y.entity.User;
import com.y.entity.vo.EChartsData;
import com.y.entity.vo.R;
import com.y.entity.vo.UserAndRole;
import com.y.service.FoodService;
import com.y.service.KindService;
import com.y.service.OrderService;
import com.y.service.UserService;
import com.y.utils.ConstantName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: Yjh
 */
@Controller
@RequestMapping("/console")
public class ConsoleController {

    @Autowired
    UserService userService;
    @Autowired
    OrderService orderService;
    @Autowired
    FoodService foodService;
    @Autowired
    KindService kindService;

    @GetMapping("/console2")
    public String console(Model model){
        int foodNums = foodService.count(new QueryWrapper<Food>().eq("status", ConstantName.GENERAL_NORMAL_STATUS_CODE)); // 菜品数量
        int foodKinds = kindService.count(new QueryWrapper<Kind>().eq("status", ConstantName.GENERAL_NORMAL_STATUS_CODE)); // 菜品种类
        BigDecimal income=BigDecimal.ZERO; // 昨日收入

        List<Order> orders = orderService.showAllOrderByStatus(ConstantName.ORDER_BUYING_STATUS);
        for (Order order : orders) {
            BigDecimal fPrice = order.getFPrice();
            fPrice.multiply(new BigDecimal(order.getNums()));
            income = income.add(fPrice);
        }

        System.out.println("foodNums = " + foodNums);
        System.out.println("foodKinds = " + foodKinds);
        System.out.println("income = " + income);
        model.addAttribute("foodNums",foodNums);
        model.addAttribute("foodKinds",foodKinds);
        model.addAttribute("userNums",userService.list().size());
        model.addAttribute("income",income);

        return "view/console/console2";
    }


    /**
     * 获取食品种类
     * @return
     */
    @GetMapping("/foodKindData")
    @ResponseBody
    public R foodKindData(){
        List<EChartsData> eChartsData = new ArrayList<>();
        List<Food> foodList = foodService.getAllFoodList();
        List<Kind> kindList = kindService.list();

        for (Kind kind : kindList) {
            String name = kind.getName();
            long count = foodList.stream()
                                 .filter((food) -> name.equals(food.getKName()))
                                 .count();
            eChartsData.add(new EChartsData(name, (int) count));
        }

        return R.success().setData(eChartsData);
    }


    /**
     * 获取系统角色
     * @return
     */
    @GetMapping("/userRecords")
    @ResponseBody
    public R userRecords(){
        int normal = 0; // 正常用户
        int admin = 0; // 管理员
        int boss = 0;   // 领导
        List<User> userList = userService.getListUser();
        for (User user : userList) {
            if ( user.getRoleId() == 1 ){
                boss++;
                continue;
            }
            if ( user.getRoleId() == 2 ){
                admin++;
                continue;
            }
            if ( user.getRoleId() == 3 ){
                normal++;
            }
        }

        // 数据
        List<EChartsData> eChartsData = new ArrayList<>();
        eChartsData.add(new EChartsData("最高领导",boss));
        eChartsData.add(new EChartsData("管理员",admin));
        eChartsData.add(new EChartsData("普通用户",normal));

        return R.success().setData(eChartsData);
    }
}
